<?php
class ProductListWidget extends CWidget
{
    public $category_id = '';
    public $page_size = 12;
    public $content_limit = 50;

    public function run()
    {
        $this->page_size = intval($this->page_size);
        $this->content_limit = intval($this->content_limit);
        
        $criteria = new CDbCriteria();
        $criteria->with = array(
            'localeProduct' => array(
                'condition' => 'locale=:locale',
                'params' => array(':locale' => Yii::app()->language)
            ),
        );
        $criteria->compare('t.status',Product::STATUS_ACTIVE);
        $criteria->order = 't.create_time DESC, t.id DESC';
        if ($this->category_id!='')
        {
            $res = FSM::run('Core.Category.getRootAndChildrenIds',array('id'=>$this->category_id,'status'=>1));
            $catIds = $res->ids;
            $criteria->addInCondition('t.category_id', $catIds);   
        }
        
        //$criteria->select = 't.id';
        $countProduct = Product::model()->count($criteria);
        $paging = new CPagination($countProduct);
        $paging->pageSize = $this->page_size;
        $paging->applyLimit($criteria);
        
        $models = Product::model()->findAll($criteria);
        $this->render('product-list',array('models'=>$models,'paging'=>$paging));
    }
}
